package com.changgou.goods.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author Sherlock
 * @date 2020/3/29 下午 1:12
 *
 * Spring Boot项目中如果集成了Spring Security在不做额外配置的情况下Swagger2文档会被拦截。
 * 解决方法是在Security的配置类中重写configure方法添加白名单即可:
 * @Override
 * public void configure ( WebSecurity web) throws Exception {
 *     web.ignoring()
 *       .antMatchers("/swagger-ui.html")
 *       .antMatchers("/v2/**")
 *       .antMatchers("/swagger-resources/**");
 * }
 */
@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    /**
     * api接口包扫描路径
     */
    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.changgou.goods";

    public static final String VERSION = "1.0.0";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                .paths(PathSelectors.any()) // 可以根据url路径设置哪些请求加入文档，忽略哪些请求
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("商品管理") //设置文档的标题
                .description("商品管理服务 API 接口文档") // 设置文档的描述
                .version(VERSION) // 设置文档的版本信息-> 1.0.0 Version information
                .termsOfServiceUrl("/") // 设置文档的License信息->1.3 License information
                .build();
    }
}